package repository

import (
	"context"
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/repository/dao"
)

type ExperimentRepository struct {
	dao *dao.ExperimentDAO
}

func NewExperimentRepository(dao *dao.ExperimentDAO) *ExperimentRepository {
	return &ExperimentRepository{
		dao: dao,
	}
}

func (r *ExperimentRepository) Query(ctx context.Context, a domain.Experiment) ([]domain.Experiment, error) {
	Experiments, err := r.dao.Query(ctx, dao.Experiment{
		Name: a.Name,
	})

	if err != nil {
		return []domain.Experiment{}, err
	}

	returnValue := make([]domain.Experiment, 0)
	for _, v := range Experiments {
		returnValue = append(returnValue, domain.Experiment{
			Id:            v.Id,
			Name:          v.Name,
			NameEn:        v.NameEn,
			Description:   v.Description,
			DescriptionEn: v.DescriptionEn,
			IfEnabled:     v.IfEnabled,
		})
	}

	return returnValue, nil
}

func (r *ExperimentRepository) Delete(ctx context.Context, ids []string) (int64, error) {
	affectedRows, err := r.dao.Delete(ctx, ids)

	if int64(len(ids)) != affectedRows {
		return 0, err
	}

	return affectedRows, nil
}

func (r *ExperimentRepository) QueryModuleTest(ctx context.Context, ModuleId string) ([]domain.Question, error) {
	Questions, err := r.dao.QueryModuleTest(ctx, ModuleId)

	if err != nil {
		return []domain.Question{}, nil
	}

	returnValue := make([]domain.Question, 0)
	for _, v := range Questions {
		returnValue = append(returnValue, domain.Question{
			Content:   v.Content,
			ContentEn: v.ContentEn,
			Answer:    v.Answer,
			Analysis:  v.Analysis,
		})
	}

	return returnValue, nil
}

func (r *ExperimentRepository) Create(ctx context.Context, a domain.Experiment) (int64, error) {
	rows, err := r.dao.Create(ctx, dao.Experiment{
		Name:          a.Name,
		NameEn:        a.NameEn,
		Chemicals:     a.Chemicals,
		Equipments:    a.Equipments,
		Consumables:   a.Consumables,
		IfEnabled:     a.IfEnabled,
		Description:   a.Description,
		DescriptionEn: a.DescriptionEn,
	})

	if err != nil {
		return 0, err
	}

	return rows, nil
}

func (r *ExperimentRepository) QueryRelatedModule(ctx context.Context, a domain.Experiment) ([]domain.CommonItem, error) {
	CommonItems, err := r.dao.QueryRelatedModule(ctx, dao.Experiment{
		Id: a.Id,
	})

	if err != nil {
		return []domain.CommonItem{}, err
	}

	returnValue := make([]domain.CommonItem, 0)
	for _, v := range CommonItems {
		returnValue = append(returnValue, domain.CommonItem{
			Id:            v.Id,
			CategoryName:  v.CategoryName,
			ModuleName:    v.ModuleName,
			ModuleNameEn:  v.ModuleNameEn,
			Description:   v.Description,
			DescriptionEn: v.DescriptionEn,
			Content:       v.Content,
			ContentEn:     v.ContentEn,
		})
	}

	return returnValue, nil
}

func (r *ExperimentRepository) QueryNeedToLearnModule(ctx context.Context, a domain.Experiment) ([]domain.CommonItem, error) {
	CommonItems, err := r.dao.QueryNeedToLearnModule(ctx, dao.Experiment{
		Id: a.Id,
	})

	if err != nil {
		return []domain.CommonItem{}, err
	}

	returnValue := make([]domain.CommonItem, 0)
	for _, v := range CommonItems {
		returnValue = append(returnValue, domain.CommonItem{
			Id:                   v.Id,
			CategoryName:         v.CategoryName,
			ModuleName:           v.ModuleName,
			ModuleNameEn:         v.ModuleNameEn,
			Score:                v.Score,
			IfPass:               v.IfPass,
			Content:              v.Content,
			ContentEn:            v.ContentEn,
			Description:          v.Description,
			DescriptionEn:        v.DescriptionEn,
			EverOccurredAccident: v.EverOccurredAccident,
		})
	}

	return returnValue, nil
}
